package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.api.dto.request.MessagesSendRequest;
import com.smedi.ismedi.inmail.api.dto.request.QueryListTaskRecordRequest;
import com.smedi.ismedi.inmail.api.dto.request.TaskRecordDeleteRequest;
import com.smedi.ismedi.inmail.api.dto.response.MessageRecordResponse;
import com.smedi.ismedi.inmail.api.dto.response.TaskRecordResponse;
import com.smedi.ismedi.inmail.api.feign.RemoteMessageFeignApi;
import com.smedi.ismedi.progress.core.application.command.MessagesSendPostCommand;
import com.smedi.ismedi.progress.core.application.command.QueryListTaskRecordCommand;
import com.smedi.ismedi.progress.core.application.command.TaskRecordCreateCommand;
import com.smedi.ismedi.progress.core.application.command.TaskRecordEditCommand;
import com.smedi.ismedi.progress.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.service.TaskRemoteService;
import com.smedi.ismedi.inmail.api.dto.request.TaskRecordRequest;
import com.smedi.ismedi.inmail.api.feign.RemoteTaskFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class TaskRemoteServiceImpl implements TaskRemoteService {

    private final RemoteTaskFeignApi remoteTaskFeignApi;
    private final AdapterConverter adapterConverter;
    private final RemoteMessageFeignApi remoteMessageFeignApi;

    @Override
    public void createTask(TaskRecordCreateCommand taskRecordCreateCommand) {
        try {
            TaskRecordRequest taskRecordRequest = adapterConverter.taskRecordCreateCommandToTaskRecordRequest(taskRecordCreateCommand);
            ResponseData<TaskRecordResponse> taskRecordResponse =  remoteTaskFeignApi.postAddTask(taskRecordRequest);
            if (!taskRecordResponse.isSuccess()){
                throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, taskRecordResponse.getApplication(), String.valueOf(taskRecordResponse.getErrorMsg()));
            }
        } catch (Exception ignore) {
        }
    }

    @Override
    public void editTask(TaskRecordEditCommand taskRecordEditCommand) {
        try {
            TaskRecordRequest taskRecordRequest = adapterConverter.taskRecordEditCommandToTaskRecordRequest(taskRecordEditCommand);
            ResponseData<Boolean> postUpdateTask =   remoteTaskFeignApi.postUpdateTask(taskRecordRequest);
            if (!postUpdateTask.isSuccess()){
                throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, postUpdateTask.getApplication(), String.valueOf(postUpdateTask.getErrorMsg()));
            }
        } catch (Exception ignore) {

        }
    }

    @Override
    public void postSendMessage(MessagesSendPostCommand messagesSendPostCommand) {
        try {
            MessagesSendRequest messagesSendRequest = adapterConverter.messagesSendPostCommandToMessagesSendRequest(messagesSendPostCommand);
            ResponseData<List<MessageRecordResponse>>  messageRecordResponse = remoteMessageFeignApi.postSendMessage(messagesSendRequest);
            if (!messageRecordResponse.isSuccess()){
                throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, messageRecordResponse.getApplication(), String.valueOf(messageRecordResponse.getErrorMsg()));
            }
        } catch (Exception ignore) {

            ignore.printStackTrace();
        }
    }

    @Override
    public List<TaskRecordResponse> queryListTaskRecord(QueryListTaskRecordCommand queryListTaskRecordCommand) {
        try {
            QueryListTaskRecordRequest taskRecordResponse = adapterConverter.queryListTaskRecordCommandToQueryListTaskRecordRequest(queryListTaskRecordCommand);
            ResponseData<List<TaskRecordResponse>> taskList = remoteTaskFeignApi.getQueryListTaskRecord(taskRecordResponse);
            if (!taskList.isSuccess()){
                throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, taskList.getApplication(), String.valueOf(taskList.getErrorMsg()));
            }
            return  taskList.getData();
        } catch (Exception ignore) {

        }
        return null;
    }

    @Override
    public void cancelTask(TaskRecordEditCommand taskRecordEditCommand) {
        try {
            TaskRecordRequest taskRecordRequest = adapterConverter.taskRecordEditCommandToTaskRecordRequest(taskRecordEditCommand);
            ResponseData<Boolean> taskRecordResponse = remoteTaskFeignApi.postCancelTask(taskRecordRequest);
            if (!taskRecordResponse.isSuccess()){
                throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, taskRecordResponse.getApplication(), String.valueOf(taskRecordResponse.getErrorMsg()));
            }
        } catch (Exception ignore) {
        }
    }

    @Override
    public void deleteTask(Long id,String taskType) {
        List<TaskRecordDeleteRequest> taskRecordDeleteRequests = new ArrayList<>();
        TaskRecordDeleteRequest request = new TaskRecordDeleteRequest();
        request.setSourceFromId(id);
        request.setTaskType(taskType);
        taskRecordDeleteRequests.add(request);
        remoteTaskFeignApi.postDeleteTaskBySources(taskRecordDeleteRequests);
    }
}
